/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeler.setup;

import java.text.NumberFormat;
import java.util.ArrayList;

import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import fi.honkalampisaatio.lmodeller.xml.BlockData;

import javax.swing.JLabel;
import java.awt.Font;

public class BlocksPanel extends JPanel {
	private JSpinner tfBlack2x2Count;
	private JSpinner tfBlue2x2Count;
	private JSpinner tfGreen2x2Count;
	private JSpinner tfRed2x2Count;
	private JSpinner tfLightGreen2x2Count;
	private JSpinner tfYellow2x2Count;
	private JSpinner tfWhite2x2Count;
	private JSpinner tfOrange2x2Count;
	private JSpinner tfTransBlue2x2Count;
	private JSpinner tfBlack4x2Count;
	private JSpinner tfBlue4x2Count;
	private JSpinner tfGreen4x2Count;
	private JSpinner tfRed4x2Count;
	private JSpinner tfLightGreen4x2Count;
	private JSpinner tfYellow4x2Count;
	private JSpinner tfWhite4x2Count;
	private JSpinner tfOrange4x2Count;
	private JSpinner tfTransBlue4x2Count;
	private JSpinner tfBlack4x2PlateCount;
	private JSpinner tfRed4x2PlateCount;

	/**
	 * Create the panel.
	 */
	public BlocksPanel() {
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("fill:default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,}));
		
		JLabel lblChoosedBlocks = new JLabel("Valitut palat");
		lblChoosedBlocks.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(lblChoosedBlocks, "2, 2, 11, 1, center, default");
		
		ImagePanel pbBlack2x2 = new ImagePanel();
		add(pbBlack2x2, "2, 4, fill, fill");
		pbBlack2x2.setImage("./images/black2x2.PNG");
		
		tfBlack2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfBlack2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfBlack2x2Count, "4, 4, fill, default");
		
		ImagePanel pbRed2x2 = new ImagePanel();
		add(pbRed2x2, "6, 4, fill, fill");
		pbRed2x2.setImage("./images/red2x2.PNG");
		
		tfRed2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfRed2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfRed2x2Count, "8, 4, fill, default");
		
		ImagePanel pbWhite2x2 = new ImagePanel();
		add(pbWhite2x2, "10, 4, fill, fill");
		pbWhite2x2.setImage("./images/white2x2.PNG");
		
		tfWhite2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfWhite2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfWhite2x2Count, "12, 4, fill, default");
		
		ImagePanel pbBlue2x2 = new ImagePanel();
		add(pbBlue2x2, "2, 6, fill, fill");
		pbBlue2x2.setImage("./images/blue2x2.PNG");
		
		tfBlue2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfBlue2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfBlue2x2Count, "4, 6, fill, default");
		
		ImagePanel pbLightGreen2x2 = new ImagePanel();
		add(pbLightGreen2x2, "6, 6, fill, fill");
		pbLightGreen2x2.setImage("./images/lightgreen2x2.PNG");
		
		tfLightGreen2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfLightGreen2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfLightGreen2x2Count, "8, 6, fill, default");
		
		ImagePanel pbOrange2x2 = new ImagePanel();
		add(pbOrange2x2, "10, 6, fill, fill");
		pbOrange2x2.setImage("./images/orange2x2.PNG");
		
		tfOrange2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfOrange2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfOrange2x2Count, "12, 6, fill, default");
		
		ImagePanel pbGreen2x2 = new ImagePanel();
		add(pbGreen2x2, "2, 8, fill, fill");
		pbGreen2x2.setImage("./images/green2x2.PNG");
		
		tfGreen2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfGreen2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfGreen2x2Count, "4, 8, fill, default");
		
		ImagePanel pbYellow2x2 = new ImagePanel();
		add(pbYellow2x2, "6, 8, fill, fill");
		pbYellow2x2.setImage("./images/yellow2x2.PNG");
		
		tfYellow2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfYellow2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfYellow2x2Count, "8, 8, fill, default");
		
		ImagePanel pbTransBlue2x2 = new ImagePanel();
		add(pbTransBlue2x2, "10, 8, fill, fill");
		pbTransBlue2x2.setImage("./images/transblue2x2.PNG");
		
		tfTransBlue2x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfTransBlue2x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfTransBlue2x2Count, "12, 8, fill, default");
		
		ImagePanel pbBlack4x2 = new ImagePanel();
		add(pbBlack4x2, "2, 10, fill, fill");
		pbBlack4x2.setImage("./images/black4x2.PNG");
		
		tfBlack4x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfBlack4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfBlack4x2Count, "4, 10, fill, default");
		
		ImagePanel pbRed4x2 = new ImagePanel();
		add(pbRed4x2, "6, 10, fill, fill");
		pbRed4x2.setImage("./images/red4x2.PNG");
		
		tfRed4x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfRed4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfRed4x2Count, "8, 10, fill, default");
		
		ImagePanel pbWhite4x2 = new ImagePanel();
		add(pbWhite4x2, "10, 10, fill, fill");
		pbWhite4x2.setImage("./images/white4x2.PNG");
		
		tfWhite4x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfWhite4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfWhite4x2Count, "12, 10, fill, default");
		
		ImagePanel pbBlue4x2 = new ImagePanel();
		add(pbBlue4x2, "2, 12, fill, fill");
		pbBlue4x2.setImage("./images/blue4x2.PNG");
		
		tfBlue4x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfBlue4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfBlue4x2Count, "4, 12, fill, default");
		
		ImagePanel pbLightGreen4x2 = new ImagePanel();
		add(pbLightGreen4x2, "6, 12, fill, fill");
		pbLightGreen4x2.setImage("./images/lightgreen4x2.PNG");
		
		tfLightGreen4x2Count =new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfLightGreen4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfLightGreen4x2Count, "8, 12, fill, default");
		
		ImagePanel pbOrange4x2 = new ImagePanel();
		add(pbOrange4x2, "10, 12, fill, fill");
		pbOrange4x2.setImage("./images/orange4x2.PNG");
		
		tfOrange4x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfOrange4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfOrange4x2Count, "12, 12, fill, default");
		
		ImagePanel pbGreen4x2 = new ImagePanel();
		add(pbGreen4x2, "2, 14, fill, fill");
		pbGreen4x2.setImage("./images/green4x2.PNG");
		
		tfGreen4x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfGreen4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfGreen4x2Count, "4, 14, fill, default");
		
		ImagePanel pbYellow4x2 = new ImagePanel();
		add(pbYellow4x2, "6, 14, fill, fill");
		pbYellow4x2.setImage("./images/yellow4x2.PNG");
		
		tfYellow4x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfYellow4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfYellow4x2Count, "8, 14, fill, default");
		
		ImagePanel pbTransBlue4x2 = new ImagePanel();
		add(pbTransBlue4x2, "10, 14, fill, fill");
		pbTransBlue4x2.setImage("./images/transblue4x2.PNG");
		
		tfTransBlue4x2Count = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfTransBlue4x2Count.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfTransBlue4x2Count, "12, 14, fill, default");
		
		ImagePanel pbBlack4x2plate = new ImagePanel();
		add(pbBlack4x2plate, "2, 16, fill, fill");
		pbBlack4x2plate.setImage("./images/black4x2plate.PNG");
		
		tfBlack4x2PlateCount = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfBlack4x2PlateCount.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfBlack4x2PlateCount, "4, 16, fill, default");
		
		ImagePanel pbRed4x2Plate = new ImagePanel();
		add(pbRed4x2Plate, "6, 16, fill, fill");
		pbRed4x2Plate.setImage("./images/red4x2plate.PNG");
		
		tfRed4x2PlateCount = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
		tfRed4x2PlateCount.setFont(new Font("Tahoma", Font.PLAIN, 20));
		add(tfRed4x2PlateCount, "8, 16, fill, default");
		resetBlocks();
	}

	protected void resetBlocks()
	{
		tfBlack2x2Count.setValue(0);
		tfBlue2x2Count.setValue(0);
		tfGreen2x2Count.setValue(0);
		tfRed2x2Count.setValue(0);
		tfYellow2x2Count.setValue(0);
		tfWhite2x2Count.setValue(0);
		tfOrange2x2Count.setValue(0);
		tfTransBlue2x2Count.setValue(0);
		tfLightGreen2x2Count.setValue(0);

		tfBlack4x2Count.setValue(0);
		tfBlue4x2Count.setValue(0);
		tfGreen4x2Count.setValue(0);
		tfRed4x2Count.setValue(0);
		tfYellow4x2Count.setValue(0);
		tfWhite4x2Count.setValue(0);
		tfOrange4x2Count.setValue(0);
		tfTransBlue4x2Count.setValue(0);
		tfLightGreen4x2Count.setValue(0);
		
		tfBlack4x2PlateCount.setValue(0);
		tfRed4x2PlateCount.setValue(0);
	}
	
	public void setData(ArrayList<BlockData> blocks) {
		resetBlocks();
		for(int i=0; blocks!=null && i<blocks.size(); i++)
		{
			BlockData bd = blocks.get(i);
			if(bd.getType().equalsIgnoreCase("BASIC2x2"))
			{
				if(bd.getColor().equalsIgnoreCase("BLACK"))
				{
					tfBlack2x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("BLUE"))
				{
					tfBlue2x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("GREEN"))
				{
					tfGreen2x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("LIGHTGREEN"))
				{
					tfLightGreen2x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("RED"))
				{
					tfRed2x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("YELLOW"))
				{
					tfYellow2x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("WHITE"))
				{
					tfWhite2x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("ORANGE"))
				{
					tfOrange2x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("TRANSBLUE"))
				{
					tfTransBlue2x2Count.setValue(bd.getCount());
				}
			}
			else if(bd.getType().equalsIgnoreCase("BASIC4x2"))
			{
				if(bd.getColor().equalsIgnoreCase("BLACK"))
				{
					tfBlack4x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("BLUE"))
				{
					tfBlue4x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("GREEN"))
				{
					tfGreen4x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("LIGHTGREEN"))
				{
					tfLightGreen4x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("RED"))
				{
					tfRed4x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("YELLOW"))
				{
					tfYellow4x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("WHITE"))
				{
					tfWhite4x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("ORANGE"))
				{
					tfOrange4x2Count.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("TRANSBLUE"))
				{
					tfTransBlue4x2Count.setValue(bd.getCount());
				}
			}
			else if(bd.getType().equalsIgnoreCase("PLATE4x2"))
			{
				if(bd.getColor().equalsIgnoreCase("BLACK"))
				{
					tfBlack4x2PlateCount.setValue(bd.getCount());
				}
				else if(bd.getColor().equalsIgnoreCase("RED"))
				{
					tfRed4x2PlateCount.setValue(bd.getCount());
				}
			}
		}
	}

	public ArrayList<BlockData> getBlocks() {
		ArrayList<BlockData> blocks = new ArrayList<BlockData>();
		//////// BASIC2x2////////////////////////////
		BlockData black2x2 = new BlockData();
		black2x2.setCount((int)tfBlack2x2Count.getValue());
		black2x2.setColor("BLACK");
		black2x2.setType("BASIC2x2");
		blocks.add(black2x2);
		
		BlockData blue2x2 = new BlockData();
		blue2x2.setCount((int)tfBlue2x2Count.getValue());
		blue2x2.setColor("BLUE");
		blue2x2.setType("BASIC2x2");
		blocks.add(blue2x2);
		
		BlockData green2x2 = new BlockData();
		green2x2.setCount((int)tfGreen2x2Count.getValue());
		green2x2.setColor("GREEN");
		green2x2.setType("BASIC2x2");
		blocks.add(green2x2);
		
		BlockData lightGreen2x2 = new BlockData();
		lightGreen2x2.setCount((int)tfLightGreen2x2Count.getValue());
		lightGreen2x2.setColor("LIGHTGREEN");
		lightGreen2x2.setType("BASIC2x2");
		blocks.add(lightGreen2x2);
		
		BlockData red2x2 = new BlockData();
		red2x2.setCount((int)tfRed2x2Count.getValue());
		red2x2.setColor("RED");
		red2x2.setType("BASIC2x2");
		blocks.add(red2x2);
		
		BlockData yellow2x2 = new BlockData();
		yellow2x2.setCount((int)tfYellow2x2Count.getValue());
		yellow2x2.setColor("YELLOW");
		yellow2x2.setType("BASIC2x2");
		blocks.add(yellow2x2);
		
		BlockData white2x2 = new BlockData();
		white2x2.setCount((int)tfWhite2x2Count.getValue());
		white2x2.setColor("WHITE");
		white2x2.setType("BASIC2x2");
		blocks.add(white2x2);
		
		BlockData orange2x2 = new BlockData();
		orange2x2.setCount((int)tfOrange2x2Count.getValue());
		orange2x2.setColor("ORANGE");
		orange2x2.setType("BASIC2x2");
		blocks.add(orange2x2);
		
		BlockData transBlue2x2 = new BlockData();
		transBlue2x2.setCount((int)tfTransBlue2x2Count.getValue());
		transBlue2x2.setColor("TRANSBLUE");
		transBlue2x2.setType("BASIC2x2");
		blocks.add(transBlue2x2);
		//////// BASIC4x2////////////////////////////
		BlockData black4x2 = new BlockData();
		black4x2.setCount((int)tfBlack4x2Count.getValue());
		black4x2.setColor("BLACK");
		black4x2.setType("BASIC4x2");
		blocks.add(black4x2);
		
		BlockData blue4x2 = new BlockData();
		blue4x2.setCount((int)tfBlue4x2Count.getValue());
		blue4x2.setColor("BLUE");
		blue4x2.setType("BASIC4x2");
		blocks.add(blue4x2);
		
		BlockData green4x2 = new BlockData();
		green4x2.setCount((int)tfGreen4x2Count.getValue());
		green4x2.setColor("GREEN");
		green4x2.setType("BASIC4x2");
		blocks.add(green4x2);
		
		BlockData lightGreen4x2 = new BlockData();
		lightGreen4x2.setCount((int)tfLightGreen4x2Count.getValue());
		lightGreen4x2.setColor("LIGHTGREEN");
		lightGreen4x2.setType("BASIC4x2");
		blocks.add(lightGreen4x2);
		
		BlockData red4x2 = new BlockData();
		red4x2.setCount((int)tfRed4x2Count.getValue());
		red4x2.setColor("RED");
		red4x2.setType("BASIC4x2");
		blocks.add(red4x2);
		
		BlockData yellow4x2 = new BlockData();
		yellow4x2.setCount((int)tfYellow4x2Count.getValue());
		yellow4x2.setColor("YELLOW");
		yellow4x2.setType("BASIC4x2");
		blocks.add(yellow4x2);
		
		BlockData white4x2 = new BlockData();
		white4x2.setCount((int)tfWhite4x2Count.getValue());
		white4x2.setColor("WHITE");
		white4x2.setType("BASIC4x2");
		blocks.add(white4x2);
		
		BlockData orange4x2 = new BlockData();
		orange4x2.setCount((int)tfOrange4x2Count.getValue());
		orange4x2.setColor("ORANGE");
		orange4x2.setType("BASIC4x2");
		blocks.add(orange4x2);
		
		BlockData transBlue4x2 = new BlockData();
		transBlue4x2.setCount((int)tfTransBlue4x2Count.getValue());
		transBlue4x2.setColor("TRANSBLUE");
		transBlue4x2.setType("BASIC4x2");
		blocks.add(transBlue4x2);
		//////// BASIC4x2////////////////////////////
		BlockData black4x2plate = new BlockData();
		black4x2plate.setCount((int)tfBlack4x2PlateCount.getValue());
		black4x2plate.setColor("BLACK");
		black4x2plate.setType("PLATE4x2");
		blocks.add(black4x2plate);

		BlockData red4x2Plate = new BlockData();
		red4x2Plate.setCount((int)tfRed4x2PlateCount.getValue());
		red4x2Plate.setColor("RED");
		red4x2Plate.setType("PLATE4x2");
		blocks.add(red4x2Plate);
		return blocks;
	}


}
